package com.wt.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wt.admin.entity.*;
import com.wt.admin.service.WqunTagService;
import com.wt.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author wt
 * @since 2023-08-29
 */
@Api(tags = "标签管理模块")
@RestController
@RequestMapping("/wqunTag")
@CrossOrigin
public class WqunTagController {

    @Autowired
    private WqunTagService wqunTagService;

    //查询所有标签
    @ApiOperation("查询所有标签")
    @GetMapping("/findAllTag")
    public R findAllTag(){
        QueryWrapper<WqunTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        List<WqunTag> findAllTag = wqunTagService.list(queryWrapper);
        return R.ok().data("findAllTag",findAllTag);
    }

    //根据姓名查询标签
    @ApiOperation("根据姓名查询标签")
    @GetMapping("/findTagByName/{name}")
    public R findTagByName(@PathVariable String name){
        QueryWrapper<WqunTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        WqunTag findTagByName = wqunTagService.getOne(queryWrapper);
        return R.ok().data("findTagByName",findTagByName);
    }

    //根据id查询标签
    @ApiOperation("根据id查询标签")
    @GetMapping("/findTagById/{id}")
    public R findTagById(@PathVariable String id){
        QueryWrapper<WqunTag> queryWrapper = new QueryWrapper<>();
        WqunTag findTagById = wqunTagService.getById(id);
        return R.ok().data("findTagById",findTagById);
    }

    //添加标签
    @ApiOperation("添加标签")
    @PostMapping("/addTag")
    public R addTag(@RequestBody WqunTag wqunTag){
        if((findTagByName(wqunTag.getName()).getData().get("findTagByName")) != null){
            return R.error().message("已存在");
        }
        System.out.println(wqunTag);
        wqunTagService.save(wqunTag);
        return R.ok().message("添加成功");
    }

}

